package app.fugema.shop.main.order.adapter;


import android.view.View;

import androidx.lifecycle.LifecycleOwner;

import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.ada.SingleRecyclerAdapter;
import com.base.common.view.adapter.connector.BaseViewHolder;
import app.fugema.shop.R;
import app.fugema.shop.databinding.OrderListItemBinding;
import app.fugema.shop.main.order.model.OrderEntity;
import app.fugema.shop.main.order.model.OrderFunBtn;
import app.fugema.shop.main.order.view.OrderDetailActivity;
import app.fugema.shop.main.order.vm.OrderListVM;

import java.util.List;

public class OrderListAdapter extends SingleRecyclerAdapter<OrderEntity, OrderListItemBinding>
{

    private OrderListVM orderListVM;//用于点击整件的网络请求

    public OrderListAdapter(LifecycleOwner lifecycleOwner, OrderListVM orderListVM)
    {
        super(lifecycleOwner);
        this.orderListVM = orderListVM;
    }


    @Override
    public int getLayoutId()
    {
        return R.layout.order_list_item;
    }

    @Override
    public void onCreateViewHolder(OrderListItemBinding binding, BaseViewHolder viewHolder)
    {
        super.onCreateViewHolder(binding, viewHolder);
        binding.rvRecyclerViewButton.setAdapter(new OrderFunButtonAdapter(getLifecycleOwner(), orderListVM));
        binding.rvRecyclerViewGoods.setAdapter(new OrderListGoodsItemAdapter(lifecycleOwner, orderListVM));
    }


    @Override
    public void onBindViewHolder(OrderListItemBinding binding, int position, BaseViewHolder viewHolder, OrderEntity bean)
    {
        super.onBindViewHolder(binding, position, viewHolder, bean);
        if (binding.rvRecyclerViewButton.getAdapter() instanceof OrderFunButtonAdapter)
        {
            OrderFunButtonAdapter adapter = (OrderFunButtonAdapter) binding.rvRecyclerViewButton.getAdapter();
            //底部的按扭  根据订单状态生成 按钮       OrderStatus 6  待自提  bean.getCommentCount()
            List<OrderFunBtn> btnInfos = OrderFunButtonAdapter.getStatesButton(bean.getOrderStatus(),bean.isCanRefund() );

            if (UIUtils.isNotEmpty(btnInfos))
            {
                for (OrderFunBtn orderFunBtn : btnInfos)
                {
                    //这是 单号  金额
                    orderFunBtn.setContent( ""+bean.getId() );
                    orderFunBtn.setTotalAmount( Double.parseDouble(bean.getOrderTotalAmount()) );
                }
            }
            adapter.setDataList(btnInfos);
        }

        if (binding.rvRecyclerViewGoods.getAdapter() instanceof OrderListGoodsItemAdapter)
        {
            OrderListGoodsItemAdapter adapter = (OrderListGoodsItemAdapter) binding.rvRecyclerViewGoods.getAdapter();
            /*
            for (CommentProductResult.OrderItemDtosBean orderItemDto : bean.getOrderItemDtos())
            {
                orderItemDto.setStatus(bean.getStatus());
                orderItemDto.setOrderNumber(bean.getOrderNumber());
            }
            */
            adapter.setDataList(bean.getItemInfo());
        }
    }

    @Override
    public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, OrderEntity bean)
    {
        super.onItemClick(view, adapter, onclickType, position, bean);
        // 点击事件
        OrderDetailActivity.start(view.getContext(), bean.getId());
    }


}
